package com.yuandian.management.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfStep;
import com.yuandian.api.management.entity.ConfStepJoinMapping;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.mapper.ConfStepJoinMappingMapper;
import com.yuandian.management.service.ConfStepJoinMappingService;
import com.yuandian.management.service.ConfStepService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 步骤异步关联配置表-Service实现
 * @author zfsn
 * @date 2023-6-1
 */
@Service
public class ConfStepJoinMappingServiceImpl extends ServiceImpl<ConfStepJoinMappingMapper, ConfStepJoinMapping>
		implements ConfStepJoinMappingService {

	@Autowired
	private ConfStepJoinMappingMapper confStepJoinMappingMapper;

	@Autowired
	private ConfStepService confStepService;
	/**
	 * 详单配置组合策略
	 *
	 * @param confStepJoinMapping 关联配置
	 * @return 通用返回对象
	 */
	@Override
	public ConfStepJoinMapping getStrategy(ConfStepJoinMapping confStepJoinMapping) {
		LambdaQueryWrapper<ConfStepJoinMapping> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.ne(ConfStepJoinMapping::getStepId, confStepJoinMapping.getStepId());
		List<ConfStepJoinMapping> confStepJoinMappings = super.list(queryWrapper);
		if (super.list(queryWrapper).size() > 0) {
			return confStepJoinMappings.get(0);
		}
		return null;
	}

	/**
	 * 保存详单配置组合策略
	 *
	 * @param confStepJoinMapping 关联配置
	 * @return 通用返回对象
	 */
	@Override
	public R saveStrategy(ConfStepJoinMapping confStepJoinMapping) {


		ConfStep confstep = confStepService.getById(confStepJoinMapping.getId());
		if(confstep==null) {
			return R.failed("配置失败，该步骤已被删除，请检查后再进行操作");
		}
		//更新步骤策略配置状态
		LambdaQueryWrapper<ConfStep> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.ne(ConfStep::getSystemId, confStepJoinMapping.getSystemId());
		queryWrapper.ne(ConfStep::getBusinessCode, confStepJoinMapping.getBusinessCode());
		List<ConfStep> confSteps = confStepService.list(queryWrapper);


		ConfStep confStep = new ConfStep();
		confStep.setIsCombinedStrategy(confStepJoinMapping.getStatus());
		confStep.setIds(new ArrayList<>());
		List<ConfStepJoinMapping> sjList = new ArrayList<>();
		if(confSteps!=null && confSteps.size()>0) {
			for(ConfStep bpvo : confSteps) {
				ConfStepJoinMapping sjVo = new ConfStepJoinMapping();
				BeanUtils.copyProperties(confStepJoinMapping, sjVo);
				sjVo.setStepId(bpvo.getId());
				sjList.add(sjVo);
				confStep.getIds().add(bpvo.getId());
			}
		}
		if(confStep.getIds()!=null && confStep.getIds().size()>0) {
			LambdaUpdateWrapper<ConfStep> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
			lambdaUpdateWrapper.eq(ConfStep::getIsCombinedStrategy, confStep.getIsCombinedStrategy());
			lambdaUpdateWrapper.set(ConfStep::getId, confStep.getIds());
			confStepService.update(lambdaUpdateWrapper);
		}
		if(confStepJoinMapping.getId()!=null) {
			//更新步骤关联策略
			confStepJoinMappingMapper.updateStep(confStepJoinMapping);
		} else {
			//入库步骤关联策略
			if(sjList.size()>0) {
				super.saveBatch(sjList);
			}
		}
		return R.ok();
	}

}